

import os
import re
import sys
import time
from datetime import datetime, timedelta

import cv2
import pyautogui
import requests
from PyQt6.QtWidgets import QApplication, QWidget
from selenium.webdriver.common.by import By

from app.configs.config import VIDEO_PATH
from app.gui.main_window import MainWindow
from app.utils.utils_kuaishou_upload import kuai_shou_upload
from app.utils.utils_selenium import (click_element, driver, get_element_value,
                                      input_element)


def run(image_base_path):

    # 开始时间（天）
    start_time_str = "2024-08-09 09:00:00"
    # 间隔时间（天）
    interval_day = 10
    # 每天整点发布时间（小时）
    up_hour_list = [5, 7, 9, 11]

    start_time = datetime.strptime(start_time_str, "%Y-%m-%d %H:%M:%S")

    up_time_list = []
    for element_interval_day in range(interval_day):
        temp_data = start_time + timedelta(days=element_interval_day)
        for up_hour in up_hour_list:
            temp_time = temp_data + timedelta(hours=up_hour)
            temp_time_str = temp_time.strftime("%Y-%m-%d %H:%M:%S")
            up_time_list.append(temp_time_str)

    # 获取待上传视频列表
    video_list = kuai_shou_upload._get_upload_list(
        VIDEO_PATH)

    # 登录
    kuai_shou_upload.login(url="https://cp.kuaishou.com/profile")
    kuai_shou_upload.save_qr_code("./images/qc_code.png")
    for element_index, video_path in enumerate(video_list):

        # 超过上传时间
        if element_index >= len(up_time_list):
            break

        # 准备上传
        kuai_shou_upload.pre_upload()

        kuai_shou_upload.select_file(video_path)
        try:
            # 点击我知道了
            click_element(
                driver, """ //button[@class="ant-btn ant-btn-primary ant-btn-sm IpMQNj-1Gqc-"] """)
        except Exception as e:
            pass

        # 输入描述
        input_element(
            driver, """ //div[@class="clGhv3UpdEo-"] """, "王者荣耀高光时刻 王者 王者荣耀 高光时刻 精彩时刻 #游戏 #王者 #王者荣耀 ")

        # 输入类型  大类
        click_element(
            driver, """ //input[@id="rc_select_0"] """)
        click_element(
            driver, """ //div[@title="游戏"] """)

        # 输入类型  小类
        click_element(
            driver, """ //input[@id="rc_select_1"] """)
        click_element(
            driver, """ //div[@title="多人在线战术竞技游戏（MOBA）"] """)

        # 设置定时发布
        click_element(
            driver, """ //span[text()="定时发布"] """)
        input_element(
            driver, """ //div[contains(@class,"ant-picker tOResR7Iq-w-")]//input """, up_time_list[element_index])
        pyautogui.sleep(1)
        pyautogui.press("enter")
        pyautogui.sleep(1)
        pyautogui.press("tab")
        pyautogui.sleep(1)

        # 发布
        click_element(
            driver, """ //button[@class="ant-btn ant-btn-primary GncXo-rrppc-"] """)
        # 确认发布
        click_element(
            driver, """ //div[@class="ant-modal-body"]//button[@class="ant-btn ant-btn-primary"] """)

        # 删除当前文件
        os.remove(video_path)
        time.sleep(2)

    print


if __name__ == "__main__":

    image_base_path = "C:/Workspace/G_01_Gitee/demo_click_text/image"

    # run(image_base_path)

    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

    # print("="*20)
    # time.sleep(10)
